﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace YJKRevitWallModule.WallModule
{
	public partial class DlgResetCorner : System.Windows.Forms.Form
	{
		public double Dist1
		{
			get
			{
				return double.Parse(this.textBoxDist1.Text);
			}
		}

		public double Dist2
		{
			get
			{
				return double.Parse(this.textBoxDist2.Text);
			}
		}

		public double Radius
		{
			get
			{
				return double.Parse(this.textBoxRadius.Text);
			}
		}

		public DlgResetCorner()
		{
			this.InitializeComponent();
			this.m_bChamfer = true;
			this.m_dDist1 = 1000.0;
			this.m_dDist2 = 1000.0;
			this.m_dRadius = 1000.0;
			this.ReadData();
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "WallCorner.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			string value = Convert.ToString(this.m_bChamfer);
			streamWriter.WriteLine(value);
			streamWriter.WriteLine(this.textBoxDist1.Text);
			streamWriter.WriteLine(this.textBoxDist2.Text);
			streamWriter.WriteLine(this.textBoxRadius.Text);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadData()
		{
			string path = Path.GetTempPath() + "WallCorner.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				return true;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			string value = streamReader.ReadLine();
			this.m_bChamfer = Convert.ToBoolean(value);
			this.radioButtonChamfer.Checked = this.m_bChamfer;
			this.radioButtonFillet.Checked = !this.m_bChamfer;
			this.textBoxDist1.Enabled = this.m_bChamfer;
			this.textBoxDist2.Enabled = this.m_bChamfer;
			this.textBoxRadius.Enabled = !this.m_bChamfer;
			this.textBoxDist1.Text = streamReader.ReadLine();
			this.textBoxDist2.Text = streamReader.ReadLine();
			this.textBoxRadius.Text = streamReader.ReadLine();
			this.m_bChamfer = this.radioButtonChamfer.Checked;
			this.m_dDist1 = ((this.textBoxDist1.Text == "") ? 0.0 : Convert.ToDouble(this.textBoxDist1.Text));
			this.m_dDist2 = ((this.textBoxDist2.Text == "") ? 0.0 : Convert.ToDouble(this.textBoxDist2.Text));
			this.m_dRadius = ((this.textBoxRadius.Text == "") ? 0.0 : Convert.ToDouble(this.textBoxRadius.Text));
			try
			{
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
			}
			catch (Exception)
			{
				streamReader.Close();
				fileStream.Close();
				return true;
			}
			streamReader.Close();
			fileStream.Close();
			return true;
		}

		private void textBoxDist1_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar != '\b' && (e.KeyChar < '0' || e.KeyChar > '9'))
			{
				e.Handled = true;
			}
		}

		private void textBoxDist2_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar != '\b' && (e.KeyChar < '0' || e.KeyChar > '9'))
			{
				e.Handled = true;
			}
		}

		private void textBoxRadius_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar != '\b' && (e.KeyChar < '0' || e.KeyChar > '9'))
			{
				e.Handled = true;
			}
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void DlgResetCorner_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				DlgResetCorner.SendMessage(CmdWallCornerSet._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			}
			else
			{
				base.Focus();
			}
			if (this.textBoxDist1.Text == "")
			{
				this.textBoxDist1.Text = this.m_dDist1.ToString();
			}
			if (this.textBoxDist2.Text == "")
			{
				this.textBoxDist2.Text = this.m_dDist2.ToString();
			}
			if (this.textBoxRadius.Text == "")
			{
				this.textBoxRadius.Text = this.m_dRadius.ToString();
			}
		}

		private void radioButtonChamfer_CheckedChanged(object sender, EventArgs e)
		{
			this.m_bChamfer = this.radioButtonChamfer.Checked;
			this.textBoxDist1.Enabled = true;
			this.textBoxDist2.Enabled = true;
			this.textBoxRadius.Enabled = false;
		}

		private void radioButtonFillet_CheckedChanged(object sender, EventArgs e)
		{
			this.m_bChamfer = !this.radioButtonFillet.Checked;
			this.textBoxDist1.Enabled = false;
			this.textBoxDist2.Enabled = false;
			this.textBoxRadius.Enabled = true;
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void DlgResetCorner_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			DlgResetCorner.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgResetCorner.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void textBoxDist1_MouseLeave(object sender, EventArgs e)
		{
			try
			{
				double dDist = double.Parse(this.textBoxDist1.Text);
				this.m_dDist1 = dDist;
			}
			catch
			{
				this.textBoxDist1.Text = this.m_dDist1.ToString();
			}
		}

		private void textBoxDist2_MouseLeave(object sender, EventArgs e)
		{
			try
			{
				double dDist = double.Parse(this.textBoxDist2.Text);
				this.m_dDist2 = dDist;
			}
			catch
			{
				this.textBoxDist2.Text = this.m_dDist2.ToString();
			}
		}

		private void textBoxRadius_MouseLeave(object sender, EventArgs e)
		{
			try
			{
				double dRadius = double.Parse(this.textBoxRadius.Text);
				this.m_dRadius = dRadius;
			}
			catch
			{
				this.textBoxRadius.Text = this.m_dRadius.ToString();
			}
		}

		public bool m_bChamfer;

		public double m_dDist1;

		public double m_dDist2;

		public double m_dRadius;
	}
}
